package TCP.ServerAndClient01;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Grup:PACKAGE_NAME
 * @Auther:cola
 * @Data:2021/4/1-2021-04-21:44
 * @version:1.0
 */
public class EchoServerDemo {
    public static void main(String[] args) {
        //创建一个服务器端的Scoket(1027-65535)
        try {
            ServerSocket server = new ServerSocket(6868);
            System.out.println("服务器已启动,正在等待客户端的连接!");
            //等待客户端的连接,造成阻塞,如果由客户端的连接成功,立即返回一个socket对象
            Socket socket = server.accept();
            System.out.println("客户端连接成功:" + server.getInetAddress().getHostAddress());
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));
            //通过输入流读取网络数据,如果没有数据,那么会造成阻塞
            String info = br.readLine();
            System.out.println(info);
            //获取输入流,向客户端返回消息
            PrintStream ps = new PrintStream(
                    new BufferedOutputStream(socket.getOutputStream())
            );
            ps.println("echo:" + info);
            ps.flush();
            //关闭
            ps.close();
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
